package top.jiaman.javaweb.dao;

import top.jiaman.javaweb.entity.User;

import java.util.List;

/**
 * @Author Jiaman
 * @Date 2020/12/3 15:53
 * @Desc
 */

public class UserDAOImpl extends BaseDAO implements UserDao {
    private static UserDAOImpl instance;

    public static final UserDAOImpl getInstance() {
        if (instance == null) {
            try {
                instance = new UserDAOImpl();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return instance;

    }

    @Override
    public int insert(User user) {
        String sql = "insert into User(id, name, password, address)values(?, ?, ?, ?)";
        int rows = update(sql, user.getId(), user.getName(), user.getPassword(), user.getAddress());
        return rows;
    }

    @Override
    public  List<User> getUser() {
        String sql = "select * from user";
        List<User> list = getForList(User.class, sql);
        return list;
    }

    @Override
    public User getUserById(String id) {
        String sql = "select id,name,password,address,phone from user where id = ?";
        User user = getInstance(User.class, sql, id);
        return user;
    }


    @Override
    public int deleteById(String id) {
        String sql = "delete from user where id = ?";
        int rows = update(sql, id);
        return rows;
    }

    @Override
    public int update(User user) {
        String sql = "update user set name = ?,password = ?,address = ?, phone = ? where id = ?";
        int rows = update(sql, user.getName(), user.getPassword(), user.getAddress(), user.getPhone(), user.getId());
        return rows;
    }

    @Override
    public int count() {
        String sql = "select count(*) from user";
        return getValue(sql);
    }

}
